package com.winderinfo.yikaotianxia.ui.videolearning;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.eventbus.SpeedEvent;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.PageAdapterUtil;
import com.winderinfo.yikaotianxia.core.video.MyIJKMediaSystem;
import com.winderinfo.yikaotianxia.core.video.MyJZMediaSystem;
import com.winderinfo.yikaotianxia.core.video.MyJZVideoPlayerStandard;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;

public class LearningActivity extends BaseActivity implements Contract.View {

    @BindView(R.id.vi_video)
    MyJZVideoPlayerStandard viVideo;
    @BindView(R.id.vi_tab)
    TabLayout viTab;
    @BindView(R.id.vi_pager)
    ViewPager viPager;

    private int id;
    private String details;
    private String photo;

    private String[] mediaName = {"普通", "高清", "原画"};
    //系统播放器引擎
    private MyJZMediaSystem mJZMediaSystem;
    private MyIJKMediaSystem mIJKMediaSystem;
    private MyJZVideoPlayerStandard myJZVideoPlayerStandard;

    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();


    @Override
    protected int initLayoutId() {
        return R.layout.activity_video;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        //学习的id
        id = intent.getIntExtra("id", 0);
        //学习介绍
        details = intent.getStringExtra("details");
        //学习图片
        photo = intent.getStringExtra("photo");
        //实例P
        presenter = new Presenter(this);
        //
        mJZMediaSystem = new MyJZMediaSystem();
        mIJKMediaSystem = new MyIJKMediaSystem();
        //切换到Ijk引擎
        JZVideoPlayer.setMediaInterface(mIJKMediaSystem);
        //设置缩略图
        Glide.with(this).load(photo).into(viVideo.thumbImageView);
        //tabList
        List<String> tablist = new ArrayList<>();
        tablist.add("目录");
        tablist.add("介绍");

        //fragment集合
        List<Fragment> fragmentlist = new ArrayList<>();
        CatalogFragment catalogFragment = new CatalogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        catalogFragment.setArguments(bundle);

        IntroduceFragment introduceFragment = new IntroduceFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putString("details", details);
        introduceFragment.setArguments(bundle);

        fragmentlist.add(catalogFragment);
        fragmentlist.add(introduceFragment);

        PageAdapterUtil pageAdapterUtil = new PageAdapterUtil(getSupportFragmentManager(), fragmentlist, tablist);
        viPager.setAdapter(pageAdapterUtil);
        viTab.setupWithViewPager(viPager);

        //接口回调的值
        catalogFragment.setOnCataUrl(new CatalogFragment.OnCataUrl() {
            @Override
            public void getId(String url) {
                initPlayerUrl(url);
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void initPlayerUrl(String url) {
        Object[] objects = new Object[3];
        LinkedHashMap map = new LinkedHashMap();
        for (int i = 0; i < 3; i++) {
            map.put(mediaName[i], url);
        }
        objects[0] = map;
        objects[1] = false;
        objects[2] = new HashMap<>();
        ((HashMap) objects[2]).put("key", "value");
        viVideo.setUp(objects, 0, JZVideoPlayer.SCREEN_WINDOW_NORMAL, "");
    }

    @Override
    public void onSuccess(Object data) {

    }

    @Override
    public void onFailure(Object error) {

    }

    @OnClick(R.id.vi_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 设置屏幕方向
     */
    private void initPlayer() {
        JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
        JZVideoPlayer.setMediaInterface(mIJKMediaSystem);
        JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
        JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }

    @Override
    protected void onResume() {
        super.onResume();
        JZVideoPlayer.setMediaInterface(mIJKMediaSystem);
        initPlayer();
    }

    @Override
    protected void onStart() {
        super.onStart();
        //注册消息总线
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);

    }

    /**
     * 倍速切换
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onMessageEventPostSpeed(SpeedEvent event) {
        mJZMediaSystem.setSpeeding(event.getSpeed());
        mIJKMediaSystem.setSpeeding(event.getSpeed());
        Toast.makeText(this, "正在切换倍速:" + event.getSpeed(), Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
